<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button
      :disabled="message.length === 0"
      class="post-btn"
      @click="pubComment"
      >发布</van-button
    >
  </div>
</template>

<script>
export default {
  name: 'CommentPost',
  data() {
    return {
      message: ''
    }
  },
  // inject: {
  //   articleId: [Number, String, Object],
  //   default: null
  // },
  props: {
    target: {
      type: [Number, String],
      required: true
    },
    articleId: {
      type: [String, Number],
      default: null
    }
  },
  methods: {
    async pubComment() {
      this.$toast.loading({
        message: '发布中...', // 提示框,提示信息
        forbidClick: true, // 禁用背景点击
        duration: 0 // 持续时间,默认两秒,如果为0,则持续展示
      })
      try {
        const { data: res } = await this.$api.pubComment({
          target: this.target, // 评论的目标id（评论文章即为文章id，对评论进行回复则为评论id）
          content: this.message, // 评论内容
          art_id: this.articleId
        })
        console.log(res)
        this.$emit('post-success', res.data)
        this.$toast('发布成功')
      } catch (err) {
        console.log(err)
        this.$toast.fail('发布失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
